package com.example.zbh.dsliao.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.db.entity.RadioSet;
import com.example.zbh.dsliao.utils.net.VolleySingleton;

import java.util.List;

/**
 * Created by yaolin on 15/11/21.
 */
public class RadioSetAdapter extends RecyclerView.Adapter<RadioSetAdapter.MyViewHolder> {
    private List<RadioSet> datas;
    private Context context;

    VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private ImageLoader imageLoader;

    public RadioSetAdapter(Context context) {
        this.context = context;
        imageLoader = volleySingleton.getImageLoader();
    }

    public void addData(List<RadioSet> datas){
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.list_hot_song_menu, null);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (datas != null && datas.size() > 0){
            RadioSet r = datas.get(position);
            holder.song.setText(r.getTitle());
            holder.descTv.setText(r.getDesc());
            if(r.getPic() != null){
                String imgUrl = r.getPic();
                getBitmap(imgUrl, holder.img);
            }
        }

    }

    private void getBitmap(String url, NetworkImageView imageView) {
        imageView.setDefaultImageResId(R.mipmap.radio_cache_icon);
        imageView.setErrorImageResId(R.mipmap.radio_cache_icon);
        imageView.setImageUrl(url, imageLoader);
    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        private NetworkImageView img;
        private TextView song, descTv;

        public MyViewHolder(View itemView) {
            super(itemView);
            img = (NetworkImageView) itemView.findViewById(R.id.img);
            song = (TextView) itemView.findViewById(R.id.song);
            descTv = (TextView) itemView.findViewById(R.id.descTv);
        }
    }
}
